#include "FpQTxtReader.h"
#include <QString>
#include <QFile>
#include <QTextStream>

FP_QFILE_BEGIN

QString FpQTxtReader::ReadTxtAll(const QString& filename)
{
	QFile file(filename);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
		return "";
	}

	QTextStream in(&file);
	QString content = in.readAll();

	file.close();

	return content;
}

int FpQTxtReader::GetTxtLineNum(const QString& filename)
{
	QFile file(filename);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
		return -1;
	}

	QTextStream in(&file);
	int lineCount = 0;
	while (!in.atEnd()) {
		in.readLine();
		lineCount++;
	}
	file.close();

	return lineCount;
}

QString FpQTxtReader::GetTxtLine(const QString& filename, int lineIndex)
{
	QFile file(filename);
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
		return "";
	}

	QTextStream in(&file);
	QString line;
	int currentLine = 0;
	while (!in.atEnd() && currentLine < lineIndex) {
		line = in.readLine();
		currentLine++;
	}
	file.close();

	if (currentLine == lineIndex) {
		return line;
	}
	else {
		return "";
	}
}

FP_QFILE_END


